package utils.https;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;

import java.net.URI;

/*
 * 利用HttpClient进行https post请求的工具类
 */
public class HttpClientUtil {

    public static String HttpGet(String url) throws Exception {
        //声明
        ProtocolSocketFactory fcty = new MySecureProtocolSocketFactory();
        URI uri = new URI(url);
        Protocol.registerProtocol("utils/https", new Protocol("utils/https", fcty, uri.getPort()));

        //然后发送请求即可
        HttpClient httpclient = new HttpClient();
        GetMethod httpget = new GetMethod(url);
        httpget.addRequestHeader("Content-type", "application/json; charset=utf-8");
        httpget.addRequestHeader("Accept", "application/json");
        try {
        	httpclient.executeMethod(httpget);
            return httpget.getResponseBodyAsString();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new Exception(ex.getMessage());
        } finally {
            httpget.releaseConnection();
        }
    }
//    public static void main(String args[]) throws Exception{
//    	String indicatorJsons = HttpGet("https://172.16.3.100:9600/HistoryTagsSync/getAll");
//    	//indicatorJsons = StringEscapeUtils.unescapeJava(indicatorJsons.substring(indicatorJsons.indexOf("["),indicatorJsons.lastIndexOf("]")+1));
//    	System.out.println(indicatorJsons);
//    }
    

}
